package com.example.music.Utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.music.R;

import java.util.List;

public class songListAdapter extends RecyclerView.Adapter<songListAdapter.ViewHolder> {
    private List<songListItem> items; // 更改为使用 songListItem 列表
    private Context context;

    public songListAdapter(Context context, List<songListItem> items) {
        this.context = context;
        this.items = items; // 将 items 赋值为传递的列表
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.songs_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return items.size(); // 使用正确的列表 items
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        songListItem item = items.get(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView iconImageView;
        public ImageView iconImageView2;

        public ViewHolder(View itemView) {
            super(itemView);
            iconImageView = itemView.findViewById(R.id.songs_item_control);
            iconImageView2 = itemView.findViewById(R.id.songs_item_like);

            iconImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = getAdapterPosition();
                    if (adapterPosition != RecyclerView.NO_POSITION) {
                        songListItem item = items.get(adapterPosition);

                        if (item.isPlay()) {
                            iconImageView.setImageResource(R.drawable.icon_play);
                        } else {
                            iconImageView.setImageResource(R.drawable.play_button);
                        }

                        notifyItemChanged(adapterPosition, "play"); // 刷新视图以更新播放图标

                        item.setPlay(!item.isPlay()); // 先更新图标状态
                    }
                }
            });

            iconImageView2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = getAdapterPosition();
                    if (adapterPosition != RecyclerView.NO_POSITION) {
                        songListItem item = items.get(adapterPosition);
                        item.setLove(!item.isLove()); // 先更新图标状态

                        if (item.isLove()) {
                            iconImageView2.setImageResource(R.drawable.icon_love_focus);
                        } else {
                            iconImageView2.setImageResource(R.drawable.icon_love);
                        }
                        notifyItemChanged(adapterPosition, "love"); // 刷新视图以更新播放图标
                    }
                }
            });
        }
    }

}
